package com.itheima.controller;

import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import com.itheima.service.impl.DeptServiceImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.logging.Logger;

@Slf4j//这样不用创建日志对象了
@RestController
@RequestMapping("/depts")
public class DeptController {

    //--------------创建log也要会
    /*//创建一个日志记录对象
    private static Logger log= (Logger) LoggerFactory.getLogger(DeptController.class);*/

    @Autowired
    private DeptService deptService;

    /*@RequestMapping(value = "/depts" ,method = RequestMethod.GET)//method的属性值限定请求方式*/
    @GetMapping//简化了限定请求方式
    public Result list() {
        log.info("查询全部部门数据");

        //调用seivice来查询部门数据
        List<Dept> deptList = deptService.list();


        return Result.success(deptList);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {

        //{}是log的占位符
        log.info("根据id删除部门:{}", id);
        deptService.delete(id);
        return Result.success();
    }

    @PostMapping
    public Result add(@RequestBody Dept dept) {//通过实体类来接受请求参数

        log.info("新增部门:{}",dept);

        deptService.add(dept);

        return Result.success(dept);
    }


}
